﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using AutoR.Messages;
using AutoR.Models;
using AutoR.Services;
using LiteDB;
using Prism.Mvvm;
using Xamarin.Forms;

namespace AutoR.Tasks
{
	// Token: 0x02000106 RID: 262
	public class FriendTask : BindableBase
	{
		// Token: 0x17000180 RID: 384
		// (get) Token: 0x0600058A RID: 1418 RVA: 0x000408F6 File Offset: 0x0003EAF6
		protected GameData Game
		{
			get
			{
				return Runtime.Game;
			}
		}

		// Token: 0x17000181 RID: 385
		// (get) Token: 0x0600058B RID: 1419 RVA: 0x00056BFE File Offset: 0x00054DFE
		// (set) Token: 0x0600058C RID: 1420 RVA: 0x00056C08 File Offset: 0x00054E08
		public string Type
		{
			[CompilerGenerated]
			get
			{
				return this.<Type>k__BackingField;
			}
			[CompilerGenerated]
			set
			{
				if (string.Equals(this.<Type>k__BackingField, value, StringComparison.Ordinal))
				{
					return;
				}
				this.<Type>k__BackingField = value;
				this.OnPropertyChanged(<>PropertyChangedEventArgs.Type);
			}
		}

		// Token: 0x17000182 RID: 386
		// (get) Token: 0x0600058D RID: 1421 RVA: 0x00056C39 File Offset: 0x00054E39
		// (set) Token: 0x0600058E RID: 1422 RVA: 0x00056C44 File Offset: 0x00054E44
		public int Id
		{
			[CompilerGenerated]
			get
			{
				return this.<Id>k__BackingField;
			}
			[CompilerGenerated]
			set
			{
				if (this.<Id>k__BackingField == value)
				{
					return;
				}
				this.<Id>k__BackingField = value;
				this.OnPropertyChanged(<>PropertyChangedEventArgs.Id);
			}
		}

		// Token: 0x17000183 RID: 387
		// (get) Token: 0x0600058F RID: 1423 RVA: 0x00056C71 File Offset: 0x00054E71
		// (set) Token: 0x06000590 RID: 1424 RVA: 0x00056C7C File Offset: 0x00054E7C
		public bool Enabled
		{
			[CompilerGenerated]
			get
			{
				return this.<Enabled>k__BackingField;
			}
			[CompilerGenerated]
			set
			{
				if (this.<Enabled>k__BackingField == value)
				{
					return;
				}
				this.<Enabled>k__BackingField = value;
				this.OnPropertyChanged(<>PropertyChangedEventArgs.Enabled);
			}
		}

		// Token: 0x17000184 RID: 388
		// (get) Token: 0x06000591 RID: 1425 RVA: 0x00056CA9 File Offset: 0x00054EA9
		// (set) Token: 0x06000592 RID: 1426 RVA: 0x00056CB4 File Offset: 0x00054EB4
		public List<int> Friend
		{
			[CompilerGenerated]
			get
			{
				return this.<Friend>k__BackingField;
			}
			[CompilerGenerated]
			set
			{
				if (object.Equals(this.<Friend>k__BackingField, value))
				{
					return;
				}
				this.<Friend>k__BackingField = value;
				this.OnPropertyChanged(<>PropertyChangedEventArgs.Friend);
			}
		}

		// Token: 0x17000185 RID: 389
		// (get) Token: 0x06000593 RID: 1427 RVA: 0x00056CE4 File Offset: 0x00054EE4
		// (set) Token: 0x06000594 RID: 1428 RVA: 0x00056CEC File Offset: 0x00054EEC
		[BsonIgnore]
		public DateTime StarTime
		{
			[CompilerGenerated]
			get
			{
				return this.<StarTime>k__BackingField;
			}
			[CompilerGenerated]
			set
			{
				if (DateTime.Equals(this.<StarTime>k__BackingField, value))
				{
					return;
				}
				this.<StarTime>k__BackingField = value;
				this.OnPropertyChanged(<>PropertyChangedEventArgs.StarTime);
			}
		}

		// Token: 0x06000595 RID: 1429 RVA: 0x00056D1C File Offset: 0x00054F1C
		public async Task Start()
		{
			List<Friend> source = await Runtime.Remote.FriendList();
			using (List<int>.Enumerator enumerator = this.Friend.ToList<int>().GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					int fri = enumerator.Current;
					if (source.All((Friend i) => i.uid != fri))
					{
						this.Friend.Remove(fri);
					}
				}
			}
			foreach (int fri2 in this.Friend)
			{
				TaskAwaiter<VisitorFriend> taskAwaiter = Runtime.Remote.FriendVisitor(fri2).GetAwaiter();
				if (!taskAwaiter.IsCompleted)
				{
					await taskAwaiter;
					TaskAwaiter<VisitorFriend> taskAwaiter2;
					taskAwaiter = taskAwaiter2;
					taskAwaiter2 = default(TaskAwaiter<VisitorFriend>);
				}
				if (taskAwaiter.GetResult().challengeNum < 3)
				{
					MessagingCenter.Send<LogMessage>(new LogMessage(string.Format("[演习]开始挑战好友{0}", fri2)), "Log");
					MessagingCenter.Send<LogMessage>(new LogMessage("[详细]补给挑战队伍"), "FullLog");
					await this.Game.SupplyFleet(this.Game.DrillTask.FleetID, 0);
					await this.Game.Delay(500);
					MessagingCenter.Send<LogMessage>(new LogMessage("[详细]开始索敌"), "FullLog");
					FoundEnemyVO spy = await Runtime.Remote.FriendSpy(fri2, this.Game.DrillTask.FleetID + 1);
					await this.Game.Delay(2000);
					List<ShipType> list = (from i in spy.enemyShips
					select i.type).ToList<ShipType>();
					MessagingCenter.Send<LogMessage>(new LogMessage("[详细]开打..."), "FullLog");
					WarReport cha;
					if (this.Game.GlobalConfig.AutoOneColume && list.Contains(ShipType.Submarine))
					{
						cha = await Runtime.Remote.FriendChallenge(fri2, this.Game.DrillTask.FleetID + 1, Formation.OneColume);
					}
					else
					{
						cha = await Runtime.Remote.FriendChallenge(fri2, this.Game.DrillTask.FleetID + 1, this.Game.DrillTask.Formation + Formation.OneRow);
					}
					await this.Game.Delay(new Random().Next(this.Game.GlobalConfig.MinSeconds * 1000, this.Game.GlobalConfig.MaxSeconds * 1000));
					MessagingCenter.Send<LogMessage>(new LogMessage("[详细]获取战斗结果"), "FullLog");
					BattleResult res = await Runtime.Remote.FriendResult(this.Game.DrillTask.IsNightWar && cha.canDoNightWar);
					await this.Game.Delay(3000);
					MessagingCenter.Send<LogMessage>(new LogMessage(string.Format("[演习]完成{0}挑战，结果:【{1}】", fri2, res.warResult.resultLevel)), "Log");
					spy = null;
					cha = null;
					res = null;
				}
			}
			List<int>.Enumerator enumerator2 = default(List<int>.Enumerator);
			DateTime dateTime = DateTime.Now.AddDays(1.0);
			this.StarTime = new DateTime(dateTime.Year, dateTime.Month, dateTime.Day, 4, 0, 0);
		}

		// Token: 0x06000596 RID: 1430 RVA: 0x00056D61 File Offset: 0x00054F61
		public FriendTask()
		{
			this.<Type>k__BackingField = "FriendTask";
			this.<Id>k__BackingField = 9;
			this.<Friend>k__BackingField = new List<int>();
			this.<StarTime>k__BackingField = DateTime.Now;
			base..ctor();
		}
	}
}
